<template>
  <section>
    <div class="banner">
      <div class="container">
        <div class="row">
          <h1 class="banner_title">Blog</h1>
          <div class="breadcrumb">
            <ul> 
              <li> <router-link :to="{path:'/'}" exact class="aui-tabBar-item " style="color: orangered;font-size: medium;">Home
                  </router-link></li>
              <li><i class="fa fa-angle-right" aria-hidden="true"></i></li>
              <li><router-link :to="{path:'/blog'}" exact class="aui-tabBar-item " style="color: orangered;font-size: medium;">Blog 
                  </router-link></li>
              
            </ul>
          </div>
        </div>
      </div>
    </div>
    
    <div id="main">
      <div id="form_box">
          <h2>有什么想要分享给大家吗？</h2>
          <textarea id="comment" cols="40" rows="10" v-model="infos"></textarea>
          <input id="img-upload" type="file" name="img" accept="image/*" @change="add_img" >
          <img id="img-show" :src="this.imgUrl[0]"  width="190" height="170" alt="上传图片" title="上传图片"> 
          <button id="send" @click="toimg()">发布</button>
      </div>
      <div id="comment_box" >

      </div>
    </div>

    <!--================ Our banner Section end =================-->
    <!--================ Welcome To CATEGORIES=================-->
    <div class="blog_left_sidebar blog_right01 cellpadding">
      <div class="container">
        <div class="row">
          <div class="col-md-12 col-sm-12">
            <div class="mainBlogContent" v-for="(v,i) in share_infos" :key='i'>
              <div class="normall blogs completeblog">
                <div class="portfolioHover">
                  <div class="image-opacity-on-hover image-zoom-on-hover">
                    <img  alt="" :src="v.share_img" id='img-sho' class="img-responsive">
                  </div>
                <div class="blogInfo">
                  <span>发布时间：{{v.create_time}} &nbsp;&nbsp;&nbsp;</span>
                  <span>发布人：{{v.user_profile_name}}</span>
                </div>
                <div class="blog_withoutside">
                  <div class="blog-content">
                    <h4 class="bloge-text">{{v.share_message}}</h4>
                  </div>
                </div>
              </div>
            </div>
          </div>
         </div>
        </div>
      </div>
    </div>

  </section>
</template>

<script>
  import {getshareList,addshareList} from '@/api/share.js'
  import {getUserId,getUserName} from '@/libs/util.js'
  export default {
    name: 'Blog',
    data() {
      return {
        infos: '',
        share_infos:{
          user_profile_id: '',
          share_img: '',
          share_message: '',
          create_time: ''
        },
        user_id: getUserId(),
        user_name: getUserName(),
        imgUrl:[],
        
      }
    },
    methods: {
      add_img(event){
        let reader = new FileReader();
        let img1 = event.target.files[0];
        
        reader.readAsDataURL(img1);
        let that=this;
        reader.onloadend=function(){
          // console.log(reader.result)
          that.imgUrl.push(reader.result)
        }
      },
      queryinfo(){
        getshareList().then( res => {
          if(res.data.status_code == 10000){
            this.share_infos = res.data.data
          }
        })
      },
      toimg(){
        // console.log(this.user_id)
        // console.log(this.infos)
        // console.log(this.imgUrl[0])
        addshareList(this.user_id,this.infos,this.imgUrl[0]).then( res => {
          if(res.data.status_code == 10000){
            alert("发布成功");
            this.share_infos.push({user_profile_name: this.user_name,share_message:this.infos,share_img:this.imgUrl[0]})
            // window.location.reload();
            this.infos = ""
            this.imgUrl = []
            // console.log(this.infos)
            // console.log(this.imgUrl)
          }
        })
      }
    },
    mounted() {
      this.queryinfo()
    }
  }
</script>

<style>
  #main {
    width: 980px;
    margin: 0 auto;
  }
   
  #form_box {
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid #cdcdcd;
  }

  #comment {
    width: 100%;
  }

  #comment_box {
    display: none;
    padding: 20px;
    border: 1px solid #cdcdcd;
  }

  #send  {
    float: right;
    width:50px;
    height: 30px;
    background-color:orange;
    border: none;
    border-radius:9%;
  }
  
  .blogInfo {
    text-align: right;
    padding-top: 0;
    padding-bottom: 15px;
    border-bottom: 1px solid #a4a4a4;
    margin-bottom: 25px;
  }
</style>
